#include <cstdio>
#include <iostream>
#include <cstring>

#include "filePipe.h"
#include "zPipe.h"

using namespace std;

int main(int args,char** argv){

    inputFilePipe iFP;
    outputFilePipe oFP;
    zDeflatePipe zDP(9);
    zInflatePipe zIP;

    iFP.file = fopen("input.txt","rb");
    oFP.file = fopen("stack.txt","wb");

    if( zDP.isError() ){
        printf("Erreur sur le pipe deflate: %s\n",zDP.getStatusString());
        return 1;
    }
    if( zIP.isError() ){
        printf("Erreur sur le pipe inflate: %s\n",zIP.getStatusString());
        return 1;
    }

    dSStatus dst;

    pipeSerie* ps = iFP >> zDP;
    *ps >> oFP;
    dst = ps->seqConsumeIS();

    if( dst == dSt_ok ) printf("OK\n");
    else{
        pipeStatus dst2 = ps->getStatus();
        const char* sdr;
        if( dst2.sender == &iFP) sdr = "Input File Pipe";
        else if( dst2.sender == &oFP) sdr = "Output File Pipe";
        else if( dst2.sender == &zDP) sdr = "Zlib Deflate Pipe";
        else sdr = "Serie";
        if( dst2.status == dSt_nullStream ) printf("Null Stream (%s)\n",sdr);
        if( dst2.status == dSt_noData ) printf("No Data on Stream (%s)\n",sdr);
        if( dst2.status == dSt_specError ){
            if( dst2.sender == &zDP) printf("Erreur: %s (%s)\n",zDP.getStatusString(),sdr);
            else printf("Erreur specifique (%s)\n",sdr);
        }
    }

    zDP.zEnd();

    fflush(oFP.file);

    fclose(iFP.file);
    fclose(oFP.file);

    delete ps;

    iFP.file = fopen("stack.txt","rb");
    oFP.file = fopen("output.txt","wb");

    ps = iFP >> zIP;
    *ps >> oFP;
    dst = ps->seqConsumeIS();

    if( dst == dSt_ok ) printf("OK\n");
    else{
        pipeStatus dst2 = ps->getStatus();
        const char* sdr;
        if( dst2.sender == &iFP) sdr = "Input File Pipe";
        else if( dst2.sender == &oFP) sdr = "Output File Pipe";
        else if( dst2.sender == &zIP) sdr = "Zlib Deflate Pipe";
        else sdr = "Serie";
        if( dst2.status == dSt_nullStream ) printf("Null Stream (%s)\n",sdr);
        if( dst2.status == dSt_noData ) printf("No Data on Stream (%s)\n",sdr);
        if( dst2.status == dSt_specError ){
            if( dst2.sender == &zIP) printf("Erreur: %s (%s)\n",zIP.getStatusString(),sdr);
            else printf("Erreur specifique (%s)\n",sdr);
        }
    }

    zIP.zEnd();

    fflush(oFP.file);

    fclose(iFP.file);
    fclose(oFP.file);

}
